<template>
    <div class="app-container">
        <el-row :gutter="10" class="mb5">



            <el-col :span="1.5">


                <el-button plain size="mini" @click="handleImport" v-hasPermi="['system:record:add']">导入</el-button>


                <el-button plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['system:record:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['system:record:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['system:record:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['system:record:export']">导出</el-button>




            </el-col>
            <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px"
                class="el-form-search">
                <!-- <el-form-item label="导入人" prop="createBy" class="el-form-search-item">
                    <el-input v-model="queryParams.createBy" placeholder="请输入导入人" clearable size="mini"
                        @keyup.enter.native="handleQuery" />
                </el-form-item> -->

                <el-form-item label="用户" prop="username" class="el-form-search-item">
                    <el-input v-model="queryParams.username" placeholder="请输入用户名称" clearable size="mini"
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="手机" prop="tel" class="el-form-search-item">
                    <el-input v-model="queryParams.tel" placeholder="请输入用户手机号" clearable size="mini"
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="导入日期" prop="rq" class="el-form-search-item">
                    <el-date-picker clearable size="mini" v-model="queryParams.rq" type="date" value-format="yyyy-MM-dd"
                        placeholder="选择导入日期">
                    </el-date-picker>
                </el-form-item>

                <el-form-item label="拨号状态" prop="username" class="el-form-search-item">
                    <el-select v-model="queryParams.called" placeholder="请选择" clearable >
                        <el-option v-for="item in [{label:'未拨',value:0},{label:'已拨',value:1}]" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item class="el-form-search-item">
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </el-row>

        <el-table :height="tableHeight" v-loading="loading" :data="recordList"
            @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <!-- <el-table-column label="导入人" align="center" prop="createUser" width="100" /> -->

            <!-- <el-table-column label="导入日期" align="center" prop="rq" width="100"> -->
                <!-- <template slot-scope="scope"> -->
                    <!-- <span>{{ parseTime(scope.row.rq, '{y}-{m}-{d}') }}</span> -->
                <!-- </template> -->
            <!-- </el-table-column> -->

            <el-table-column label="用户" align="center" prop="username" />
            <el-table-column label="手机号" align="center" prop="tel" width="100" />

            <el-table-column label="被拨" align="center" prop="called" width="50">

                <template slot-scope="scope">
                    <span>{{ scope.row.called == 1 ? '是' : '否' }}</span>
                </template>
            </el-table-column>


            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">

                <template slot-scope="scope">
                    <el-link type="primary" style="margin-right: 10px;" :href="'tel:' + scope.row.tel">拨打</el-link>
                    <el-link size="mini" type="success" style="margin-right: 10px;" @click="handleUpdate1(scope.row, 1)"
                        v-hasPermi="['system:record:edit']">已拨</el-link>
                    <el-link style="margin-right: 10px;" type="warning" size="mini" @click="handleUpdate1(scope.row, 0)"
                        v-hasPermi="['system:record:edit']">未拨</el-link>
                    <el-link style="margin-right: 10px;" type="info" size="mini" @click="handleUpdate(scope.row)"
                        v-hasPermi="['system:record:edit']">备注</el-link>
                    <el-link size="mini" type="danger" @click="handleDelete(scope.row)"
                        v-hasPermi="['system:record:remove']">删除</el-link>

                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改用户号码拨打记录对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body :close-on-click-modal="false"
            v-dialogDrag>
            <el-form ref="form" :model="form" :rules="rules" label-width="70px">
                <el-form-item label="用户" prop="username">
                    <el-input v-model="form.username" placeholder="请输入用户名称" />
                </el-form-item>
                <el-form-item label="手机号" prop="tel">
                    <el-input v-model="form.tel" placeholder="请输入用户手机号" />
                </el-form-item>
                <el-form-item label="导入日期" prop="rq">
                    <el-date-picker clearable size="mini" v-model="form.rq" type="date" value-format="yyyy-MM-dd"
                        placeholder="选择导入日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input type="textarea"
                    :rows="4"
                    v-model="form.remark" placeholder="备注信息" />
                </el-form-item>


            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>



        <!-- 导入数据 -->
        <el-dialog title="导入用户电话记录" :visible.sync="importDialog" width="90%" append-to-body
            :close-on-click-modal="false" v-dialogDrag>

            <el-upload class="upload-demo" ref="upload" :limit="1" :headers="getHeaders" :action="actionurl"
                :on-success="successCall" :auto-upload="false">
                <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
                <el-button style="margin-left: 10px;" size="small" type="success"
                    @click="submitUpload">上传到服务器</el-button>
                <div slot="tip" class="el-upload__tip">只能上传excel文件</div>
            </el-upload>



        </el-dialog>







    </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/system/record";
import { getToken } from '@/utils/auth'
export default {
    name: "Record",
    data() {
        return {
            // 表格高度
            tableHeight: document.documentElement.clientHeight - 180,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 用户号码拨打记录表格数据
            recordList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 20,
                orderByColumn: "create_time",
                isAsc: "desc",
                username: null,
                tel: null,
                rq: null,
                called: null
            },
            importDialog: false,
            actionurl: process.env.VUE_APP_BASE_API + "/system/record/importExcel",
            fileList: [],
            getHeaders: {
                Authorization: 'Bearer ' + getToken()
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        successCall(response, file, fileList) {
            console.log(response)
            this.$refs.upload.clearFiles();
            if (response.data.length > 0) {
                const result = response.data.join('<br/>');
                this.$message({
                    dangerouslyUseHTMLString: true,
                    message: result
                });
                return;
            }
            this.importDialog = false;
            this.$message({
                message: '导入成功',
                type: 'success'
            });
        },



        submitUpload() {
            this.$refs.upload.submit();
        },

        handleImport() {
            this.importDialog = true;
        },

        /** 查询用户号码拨打记录列表 */
        getList() {
            this.loading = true;
            listRecord(this.queryParams).then(response => {
                this.recordList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                createTime: null,
                updateTime: null,
                username: null,
                tel: null,
                rq: null,
                called: null,
                remark: null,
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");

            this.queryParams= {
                pageNum: 1,
                pageSize: 20,
                orderByColumn: "create_time",
                isAsc: "desc",
                username: null,
                tel: null,
                rq: null,
                called: null
            };

            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加用户号码拨打记录";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getRecord(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改用户号码拨打记录";
            });
        },

        handleUpdate1(row, called) {
            let param = { ...row }
            param.called = called;
            updateRecord(param).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.getList();
            });
        },



        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateRecord(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addRecord(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除记录？').then(function () {
                return delRecord(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('system/record/export', {
                ...this.queryParams
            }, `record_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>